package com.zidian.study.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;


import com.zidian.student.trucks.R;
import com.zidian.study.wheel.NumericWheelAdapter;
import com.zidian.study.wheel.WheelView;

import java.util.Calendar;

/**
 * Created by Administrator on 2016/9/5 0005.
 */
public class DateCheckUtil {

    public AlertDialog mDialogView;

    private WheelView year;
    private WheelView month;
    private WheelView day;
    private WheelView hour;
    private WheelView minute;
    private TextView mBtnDateConfirm;
    private TextView mBtnCancel;
    private int mYear = 0;
    private int mMonth = 0;
    private int mDay = 1;
    private int mHour = 12;
    private int mMinute = 0;
    private Context context;
    private TextView tv;

    public DateCheckUtil(Context context, TextView tv) {
        this.context = context;
        this.tv = tv;
    }


    /**
     * 生日的点击方法
     */
    public void clickBirthday(int mYear, int mMonth, int mDay) {
        this.mYear = mYear;
        this.mMonth = mMonth;
        this.mDay = mDay;
        final TextView textView = tv;
        mDialogView = CommonDialogUtil.getCommonDialog(context,
                R.layout.dialog_data, Gravity.BOTTOM);

        year = (WheelView) mDialogView.findViewById(R.id.id_province);
        month = (WheelView) mDialogView.findViewById(R.id.id_city);
        day = (WheelView) mDialogView.findViewById(R.id.id_district);
        hour = (WheelView) mDialogView.findViewById(R.id.id_hour);
        minute = (WheelView) mDialogView.findViewById(R.id.id_minute);
        mBtnDateConfirm = (TextView) mDialogView.findViewById(R.id.btn_confirm);
        mBtnCancel = (TextView) mDialogView.findViewById(R.id.btn_cancel);
        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialogView.dismiss();
            }
        });
        mBtnDateConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSelectedResult(textView);
            }
        });
        setUpDataDate();
    }

    /**
     * 更新日期数据
     **/
    private void setUpDataDate() {
        getDataPick();
        year.setVisibleItems(7);
        month.setVisibleItems(7);
        day.setVisibleItems(7);
        hour.setVisibleItems(7);
        minute.setVisibleItems(7);
    }


    /**
     * 初始化日期的数据
     */
    private void getDataPick() {
        Calendar c = Calendar.getInstance();
        int norYear = c.get(Calendar.YEAR) + 1;
        int starrtYear = c.get(Calendar.YEAR);

        int curYear = mYear;
        int curMonth = mMonth + 1;
        int curDate = mDay;
        int curHour = mHour;
        int curMinute = mMinute;

        NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(
                context, starrtYear, norYear);
        numericWheelAdapter1.setLabel("年");
        year.setViewAdapter(numericWheelAdapter1);

        NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(
                context, 1, 12, "%02d");
        numericWheelAdapter2.setLabel("月");
        month.setViewAdapter(numericWheelAdapter2);
        month.setCyclic(true);

        initDay(curYear, curMonth);
        day.setCyclic(true);

        NumericWheelAdapter numericWheelAdapter3 = new NumericWheelAdapter(
                context, 1, 24, "%02d");
        numericWheelAdapter3.setLabel("点");
        hour.setViewAdapter(numericWheelAdapter3);
        hour.setCyclic(true);
        NumericWheelAdapter numericWheelAdapter4 = new NumericWheelAdapter(
                context, 0, 59, "%02d");
        numericWheelAdapter4.setLabel("分");
        minute.setViewAdapter(numericWheelAdapter4);
        minute.setCyclic(true);

        year.setCurrentItem(curYear - norYear);
        month.setCurrentItem(curMonth - 1);
        day.setCurrentItem(curDate - 1);
        hour.setCurrentItem(curHour - 1);
        minute.setCurrentItem(curMinute);

    }

    /**
     * 得到当个月的天数
     *
     * @param year
     * @param month
     * @return
     */
    private int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }

    /**
     * 初始化日期
     */
    private void initDay(int arg1, int arg2) {
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(context,
                1, getDay(arg1, arg2), "%02d");
        numericWheelAdapter.setLabel("日");
        day.setViewAdapter(numericWheelAdapter);
    }

    /**
     * 显示最后的时间和地址的结果
     */
    private void showSelectedResult(TextView tv) {

        // YMToastUtil.YMToastCan(this, "success");
        int n_year = year.getCurrentItem() + mYear;
        int n_month = month.getCurrentItem() + 1;
        initDay(n_year, n_month);
        String birthday = new StringBuilder()
                .append((year.getCurrentItem() + mYear))
                .append("-")
                .append((month.getCurrentItem() + 1) < 10 ? "0"
                        + (month.getCurrentItem() + 1) : (month
                        .getCurrentItem() + 1))
                .append("-")
                .append(((day.getCurrentItem() + 1) < 10) ? "0"
                        + (day.getCurrentItem() + 1) : (day
                        .getCurrentItem() + 1))
                .append(" ")
                .append(((hour.getCurrentItem() + 1) < 10) ? "0"
                        + (hour.getCurrentItem() + 1) : (hour
                        .getCurrentItem() + 1))
                .append(":")
                .append(((minute.getCurrentItem()) < 10) ? "0"
                        + (minute.getCurrentItem()) : (minute
                        .getCurrentItem())).toString();
        mDialogView.dismiss();
        tv.setText(birthday);

    }


}
